﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace Kevin.Controls
{
    internal class MenuItemConverter : JavaScriptConverter
    {
        public MenuItemConverter()
        { }

        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {

            return this.MenuConvert((List<MenuItem>)obj);
           // throw new NotImplementedException();
        }

        IDictionary<string, object> MenuConvert(List<MenuItem> menuItems)
        {
            IDictionary<string, object> menu = new Dictionary<string, object>();
            foreach (MenuItem menuItem in menuItems)
            {                
                menu["ID"] = menuItem.ID;
                menu["Image"] = menuItem.Image;
                menu["Level"] = menuItem.Level;
                menu["Link"] = menuItem.Link;
                menu["PowerID"] = menuItem.PowerID;
                menu["Text"] = menuItem.Text;
                menu["CssClass"] = menuItem.CssClass;

                if (menuItem.ChildMenu.Count > 0)
                {
                    menu["ChildMenu"] = MenuConvert(menuItem.ChildMenu);
                }
                else
                {
                    menu["ChildMenu"] = "";
                }
            }
            return menu;
        }        

        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override IEnumerable<Type> SupportedTypes
        {
             get {yield return typeof(List<MenuItem>); }
        }
    }
}
